﻿//////////////////////////////////////////////////////////////////
//
// SallyConvoy.cs
//
// Copyright (c) 2012 Dan Pike. All rights reserved
//
// Refer to license.txt for any license restrictions.
// 
using Entities;
using Entities.Cities;
using Logger4Net;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Sally
{
   //////////////////////////////////////////////////////////////////////////
   /// <summary>
   /// 
   /// </summary>
   public class SallyConvoy
   {
      //////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      public SallyConvoy()
      {
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public bool initialize(World world, Main gui, string name)
      {
         try
         {
            world_ = world;
            gui_ = gui;
            name_ = name;

            item_ = gui_.viewConvoy.Items.Add(name);
            item_.Tag = this;
            item_.SubItems.Add(string.Empty); // Capacity column
            item_.SubItems.Add(string.Empty); // Required column
            return true;
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return false;
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public void shutdown()
      {
         try
         {
            if (null != item_)
            {
               gui_.viewConvoy.Items.Remove(item_);
               item_ = null;
            }
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      private void updateFieldQty()
      {
         try
         {
            item_.SubItems[1].Text = capacity_.ToString("#,#");
            item_.SubItems[2].Text = payload_.ToString("#,#");
            item_.ForeColor = ((0 < payload_) && (payload_ > capacity_)) ? Color.Red : Color.Black;
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public double getResupplyTime(City city, bool frozenHarbours)
      {
         double result = 0.0;
         try
         {
            int count = cities_.Count;
            for (int index = 0; index < count; ++index)
            {
               result += world_.getJourneyTime(cities_[index], cities_[(index + 1) % count]);
               result += 1.0;  // One day turnaround in the harbour
            }

            if (frozenHarbours && city.frozen)
            {
               // Assume a 30-day minimum until the harbour thaws
               result += 30.0;
            }
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return result;
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public bool getVisitsCity(City city)
      {
         bool result = false;
         try
         {
            result = cities_.Contains(city);
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return result;
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public bool getIsHub(City city)
      {
#warning ¬¬¬ change so that we can specify different hubs
         bool result = false;
         try
         {
            result = (world_.lubeck == city);   // hard-coded for now
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return result;
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public string name
      {
         get { return name_; }
         set { name_ = value; }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public ListViewItem item
      {
         get { return item_; }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public int capacity
      {
         get { return capacity_; }
         set
         {
            capacity_ = value;
            updateFieldQty();
         }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public int payload
      {
         get { return payload_; }
         set
         {
            payload_ = value;
            updateFieldQty();
         }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public List<City> cities
      {
         get { return cities_; }
         set { cities_ = value; }
      }

      //////////////////////////////////////////////////////////////////////////
      //
      private static Logger logger_ = Program.logger;
      private World world_;
      private Main gui_;
      private string name_;
      private int capacity_;
      private int payload_;
      private List<City> cities_;
      private ListViewItem item_;
   }
}
